﻿using Microsoft.EntityFrameworkCore;
using MyBlog2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyBlog2.Repository
{
    public class TagRepository : ITagRepository
    {
        private readonly AppDbContext _context;

        public TagRepository(AppDbContext context)
        {
            this._context = context;
        }


        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public Tag AddTag(Tag tag)
        {
            _context.Tags.Add(tag);
            _context.SaveChanges();
            return tag;
        }


        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        public bool DeleteTag(int id)
        {
            var tag = _context.Tags.FirstOrDefault(t=>t.Id == id);
            if (tag != null)
            {
                _context.Tags.Remove(tag);
                return _context.SaveChanges() > 0;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 编辑标签
        /// </summary>
        /// <param name="editTag"></param>
        /// <returns></returns>
        public Tag EditTag(Tag editTag)
        {
            var tag = _context.Tags.FirstOrDefault(t => t.Id == editTag.Id);
            if (tag != null)
            {
                tag.Name = editTag.Name;
                _context.Tags.Update(tag);
                _context.SaveChanges();
            }
            return tag;

        }

        /// <summary>
        /// 获取一个标签
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public Tag GetOneTag(int id)
        {
            return _context.Tags.FirstOrDefault(t => t.Id == id);
        }


        /// <summary>
        /// 获取一个标签(通过name)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Tag GetOneTag(string name)
        {
            return _context.Tags.FirstOrDefault(t => t.Name == name);
        }




        /// <summary>
        /// 获取全部标签
        /// </summary>
        /// <returns></returns>
        public List<Tag> GetTagList()
        {
            return _context.Tags.ToList();
        }

        /// <summary>
        /// 获取前x条
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<Tag> GetTagList(int top)
        {
            var t = _context.Tags.Include(t => t.ArticleTag).OrderByDescending(tid => tid.ArticleTag.Count).Take(top).ToList();
            return t;
        }


        /// <summary>
        /// 获取标签列表（文章数量排序）
        /// </summary>
        /// <param name="nouse"></param>
        /// <returns></returns>
        public List<Tag> GetTagList(string nouse)
        {
            return _context.Tags.Include(t => t.ArticleTag).OrderByDescending(t => t.ArticleTag.Count).ToList();
        }
    }
}
